package com.huanghuai.retail.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huanghuai.retail.common.R;
import com.huanghuai.retail.dto.SuppliersDto;
import com.huanghuai.retail.entity.Suppliers;
import com.huanghuai.retail.service.BrandService;
import com.huanghuai.retail.service.SuppliersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/suppliers")
@Slf4j
public class SuppliersController {

    @Autowired
    private SuppliersService suppliersService;

    @Autowired
    private BrandService brandService;

    /**
     * 新增供应商
     * @param
     * @return
     */
    @PostMapping("/addSuppliers")
    public R<String> addSuppliers(@RequestBody SuppliersDto suppliersDto){
        //新增供应商同时插入品牌名称和工厂名称
        Long brandId = suppliersDto.getBrandId();
        String name = suppliersDto.getName();
        Long factoryId = suppliersDto.getFactoryId();
        String area = suppliersDto.getArea();
        String phone = suppliersDto.getPhone();
        if(StringUtils.isNotEmpty(name)
                &&StringUtils.isNotEmpty(String.valueOf(brandId))
                &&StringUtils.isNotEmpty(String.valueOf(factoryId))
                &&StringUtils.isNotEmpty(area)
                &&StringUtils.isNotEmpty(phone)) {
            LambdaQueryWrapper<Suppliers> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Suppliers::getName, suppliersDto.getName());
            Suppliers one = suppliersService.getOne(queryWrapper);
            if (one == null) {

                suppliersService.save(suppliersDto);
                return R.success("新增供应商成功");
            }
            return R.error("供应商信息已存在");
        }
        return R.error("供应商信息不全，新增失败");

    }

    /**
     * 根据id删除供应商
     * @param id
     * @return
     */
    @DeleteMapping()
    public R<String> delete(Long id){

        if (id==null) {
            return R.error("删除失败");
        }
        else
            if (suppliersService.removeById(id))
            return R.success("供应商删除成功");
        else
            return R.error("供应商不存在,删除失败");
    }

    /**
     * 修改供应商信息
     * @param suppliers
     * @return
     */
    @PutMapping()
    public R<String> updateBrand(@RequestBody Suppliers suppliers){

        Suppliers suppliersById = suppliersService.getById(suppliers.getId());
        if (suppliersById==null){
            return R.error("修改失败,供应商信息不存在");
        }
        Long brandId = suppliers.getBrandId();
        String name = suppliers.getName();
        Long factoryId = suppliers.getFactoryId();
        String area = suppliers.getArea();
        String phone = suppliers.getPhone();
        if(StringUtils.isNotEmpty(name)
                &&StringUtils.isNotEmpty(String.valueOf(brandId))
                &&StringUtils.isNotEmpty(String.valueOf(factoryId))
                &&StringUtils.isNotEmpty(area)
                &&StringUtils.isNotEmpty(phone)) {

            suppliersService.updateById(suppliers);
            return R.success("修改成功  ");
        }
        return R.error("修改失败,所有都数据都不能有空");

    }


    /**
     * 分页查询所有的品牌信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/selectAllSupplies")
    public R<Page> selectAllSuppliers(@RequestParam(defaultValue= "1") int page,
                                      @RequestParam(defaultValue = "10") int pageSize){

        Page<Suppliers> suppliersPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Suppliers> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Suppliers::getId);
        suppliersService.page(suppliersPage,queryWrapper);
        List<Suppliers> records = suppliersPage.getRecords();
        List<SuppliersDto> suppliersDtoList = new ArrayList<>();
        Page<SuppliersDto> suppliersDtoPage = new Page<>();
        for (int i = 0; i <records.size(); i++) {
            suppliersDtoList.add(suppliersService.solveDto(records.get(i)));
            suppliersDtoPage.setRecords(suppliersDtoList);
        }
        return R.success(suppliersDtoPage);
    }

    /**
     * 根据供应商名称或品牌名称进行模糊查询
     * @param page
     * @param pageSize
     * @param name
     * @param brandName
     * @return
     */
    @GetMapping("/findSuppliers")
    public R<Page> findSuppliers(@RequestParam(defaultValue = "1") int page,
                                 @RequestParam(defaultValue = "10") int pageSize,
                                 String name,
                                 String brandName){
            Page<Suppliers> pageInfo = new Page<>(page,pageSize);
            LambdaQueryWrapper<Suppliers> queryWrapper = new LambdaQueryWrapper<>();
            //处理供货商名称
            queryWrapper.like(name!=null,Suppliers::getName,name);
            queryWrapper.orderByDesc(Suppliers::getId);

            //处理品牌名称
            List<Long> brandIds = null;
            if(brandName!=null){
                brandIds = brandService.getIdsByName(brandName);
            }
            if (brandIds!=null&&brandIds.size()==0) {
                brandIds.add(-1L);
            }
            queryWrapper.in(brandIds!=null, Suppliers::getBrandId, brandIds);


            suppliersService.page(pageInfo,queryWrapper);
            List<Suppliers> records = pageInfo.getRecords();
            if(records.size()==0) {
                return R.error("未查询到信息，请重新输入!");
            }
            List<SuppliersDto> suppliersDtoList = new ArrayList<>();
            Page<SuppliersDto> suppliersDtoPage = new Page<>();
            for (int i = 0; i < records.size(); i++) {
                suppliersDtoList.add(suppliersService.solveDto(records.get(i)));
            }
             BeanUtils.copyProperties(pageInfo,suppliersDtoPage);
            suppliersDtoPage.setRecords(suppliersDtoList);
            return R.success(suppliersDtoPage);
    }


    /**
     * 根据id查询信息
     * @param id
     * @return
     */
    @GetMapping("/findSupplierById/{id}")
    public R<SuppliersDto> findSupplierById(@PathVariable Long id){
        log.info("根据id查询供应商信息");
        Suppliers suppliers = suppliersService.getById(id);
        if(suppliers !=null){
            SuppliersDto suppliersDto = suppliersService.solveDto(suppliers);
            return R.success(suppliersDto);
        }
        return R.error("没有查询到对应的供应商信息");
    }

    /**
     * 启用，禁用供应商
     * @param suppliers
     * @return
     */
    @PutMapping("/update")
    public R<String> update(@RequestBody Suppliers suppliers){

        Suppliers suppliersById = suppliersService.getById(suppliers.getId());
        if (suppliersById==null){
            return R.error("状态修改,供应商信息不存在");
        }
        suppliersService.updateById(suppliers);
        return R.success("供应商状态修改成功");
    }






}
